﻿var SubPrefix = "ctl00_ctl00_ContentPlaceHolder3_ContentPlaceHolder1_";
var TotalNumberOfSubs = 20;

function CloseOpenDiv(DivToClose, DivToOpen){
    $("#" + DivToClose).hide();
    $("#" + DivToOpen).show(200);
}

function InitSubArrows() {
    var SubType;
    var j;
    for (var i = 2; i <= TotalNumberOfSubs; i++) {
        SubType = document.getElementById(SubPrefix + "Sub" + i + "TypeHiddenField").value;
        if (SubType == "0") {
            j = parseInt(i) - 1;
            $("#" + SubPrefix + "Sub" + j + "SwapDownImageButton").hide();
            $("#" + SubPrefix + "Sub" + i + "SwapUpImageButton").hide();
        }
        else {
            j = parseInt(i) - 1;
            $("#" + SubPrefix + "Sub" + j + "SwapDownImageButton").show();
            $("#" + SubPrefix + "Sub" + i + "SwapUpImageButton").show();
        }
    }
}

function GetTimeFromTillString(from, till) {
    if ((from == "0") && (till == "120")) {
        return (document.getElementById(SubPrefix + "NoSubstitutionTimeRestrictionsHiddenField").value);
    }
    if (from == "0") {
        return (document.getElementById(SubPrefix + "NoLaterThanStringHiddenField").value + " " + till + " " + document.getElementById(SubPrefix + "MinutesHiddenField").value);
    }
    if (till == "120") {
        return (document.getElementById(SubPrefix + "NotBeforeTextHiddenField").value + " " + from + " " + document.getElementById(SubPrefix + "MinutesHiddenField").value);
    }
    return (document.getElementById(SubPrefix + "NotBeforeTextHiddenField").value + " " + from + " " + document.getElementById(SubPrefix + "MinutesHiddenField").value + ", " + 
            document.getElementById(SubPrefix + "NoLaterThanStringHiddenField").value + " " + till + " " + document.getElementById(SubPrefix + "MinutesHiddenField").value);
    
}

function GetPossessionFromUptoString(from, upto) {
    if ((from == "0") && (upto == "100")) {
        return (document.getElementById(SubPrefix + "NoPossessionRestrictionsStringHiddenField").value);
    }
    if (from == "0") {
        return (document.getElementById(SubPrefix + "NoMoreThan_PercentHiddenField").value + " " + upto + " " + 
                document.getElementById(SubPrefix + "PercentHiddenField").value);
    }
    if (upto == "100") {
        return (document.getElementById(SubPrefix + "AtLeast_PercentHiddenField").value + " " + from + " " +
        document.getElementById(SubPrefix + "PercentHiddenField").value);
    }
    return (document.getElementById(SubPrefix + "BetweenHiddenField").value + " " + from + " " +
           document.getElementById(SubPrefix + "PercentAndHiddenField").value + " " + upto + " " +
           document.getElementById(SubPrefix + "PercentHiddenField").value);
}


function GetGoalsDownUpString(GoalsNumber) {
    var GN = parseInt(GoalsNumber);
    if (GN == 10) {
        return (document.getElementById(SubPrefix + "UpByStringHiddenField").value + " 10+");
    }
    if (GN == -10) {
        return (document.getElementById(SubPrefix + "DownByStringHiddenField").value + " 10+");
    }
    if (GN == 0) {
        return (document.getElementById(SubPrefix + "DrawnStringHiddenField").value);
    }
    if (GN > 0) {
        return (document.getElementById(SubPrefix + "UpByStringHiddenField").value + " " + GoalsNumber);
    }
    return (document.getElementById(SubPrefix + "DownByStringHiddenField").value + " " + (-GoalsNumber));
}
function GetGoalsConditionFromUptoString(from, upto) {

    if (from == "-10" && upto == "10") {
        return (document.getElementById(SubPrefix + "AnywayStringHiddenField").value);
    }
//    if (from == "-10") {
//        return (document.getElementById(SubPrefix + "GoalsForeMostHiddenField").value + " " + GetGoalsDownUpString(upto));
//    }
//    if (upto == "10") { 
//        return (document.getElementById(SubPrefix + "GoalsConditionAtLeastHiddenField").value + " " + GetGoalsDownUpString(from));
    //    }
    
    return (document.getElementById(SubPrefix + "BetweenHiddenField").value + " " + GetGoalsDownUpString(from) + " " + 
            document.getElementById(SubPrefix + "AndWordHiddenField").value + " " + GetGoalsDownUpString(upto));
}


function InitSubstitutionTime() {
    $("#SubstitutionTimeSlider").slider("option", "values", [0, 120]);
    document.getElementById(SubPrefix + "SubstitutionTimeLabel").innerHTML = GetTimeFromTillString(0, 120);
}

function InitPossessionCondition() {
    $("#PossessionSlider").slider("option", "values", [0, 100]);
    document.getElementById(SubPrefix + "PosesseionConditionLabel").innerHTML = GetPossessionFromUptoString(0, 100);
}

function InitGoalsCondition() {
    $("#GoalsConditionSlider").slider("option", "values", [-10, 10]);
    document.getElementById(SubPrefix + "GoalsConditionLabel").innerHTML = GetGoalsConditionFromUptoString(-10, 10);
}


function InitPlayerOutNewSubArea() {
    document.getElementById(SubPrefix + "PlayerOutDropDownList").selectedIndex = 0;
}

function InitPlayerInNewSubArea() {
    document.getElementById(SubPrefix + "PlayerInDropDownList").selectedIndex = 0;
}

function InitPlayerChangesNewSubArea() {
    document.getElementById(SubPrefix + "PlayerPositionBehaviourDropDownList").selectedIndex = 0;
    document.getElementById(SubPrefix + "PlayerInstructionDropDownList").selectedIndex = 0;
    document.getElementById(SubPrefix + "PlayerPreferredActionBehaviourDropDownList").selectedIndex = 0;
}

function InitTeamChangesNewSubArea() {
    document.getElementById(SubPrefix + "TeamNewAttitudeBehaviourDropDownList").selectedIndex = 0;
    document.getElementById(SubPrefix + "TeamAggressivenessBehaviourDropDownList").selectedIndex = 0;
}


function InitSubstitutionArea() {
    // used to init new substitution area
    InitSubstitutionTime();
    InitPossessionCondition();
    InitGoalsCondition();
    InitPlayerOutNewSubArea();
    InitPlayerInNewSubArea();
    InitPlayerChangesNewSubArea();
    InitTeamChangesNewSubArea();
}

function CancelSubCreation() {
    $("#NewSubstitution").fadeOut(200);
    $("#" + SubPrefix + "ShowSubstitutionButton").css( "color", "white" );
    $("#" + SubPrefix + "ShowPlayerChangeButton").css( "color", "white" );
    $("#" + SubPrefix + "ShowTeamChangeButton").css("color", "white");
}

function OpenNewSubstitution(SubType) {

    // close AddNewSub div
    //$("#AddNewSub").fadeOut(200);
    $("#" + SubPrefix + "ShowSubstitutionButton").css("color", "white");
    $("#" + SubPrefix + "ShowPlayerChangeButton").css("color", "white");
    $("#" + SubPrefix + "ShowTeamChangeButton").css("color", "white");
    $("#" + SubPrefix + "Show" + SubType + "Button").css("color", "yellow");

    // hide penalties area:
    $("#PenaltiesDiv").fadeOut(200);
    $("#" + SubPrefix + "ShowPenaltyTakersButton").css("color", "white");

    // clear all substitution fields
    InitSubstitutionArea();
    
    // open relevant divs
    if (SubType == "Substitution") {
        document.getElementById(SubPrefix + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "PlayerOutHiddenField").value;
        document.getElementById(SubPrefix + "NewSubstitutionTypeHiddenField").value = "1";
        $("#playerOutDiv").fadeIn();
        $("#playerInDiv").fadeIn();
        $("#playerChangeDiv").fadeIn();
        $("#teamChangeDiv").hide();        
    }
    else if (SubType == "PlayerChange") {
        document.getElementById(SubPrefix + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "PlayerHiddenField").value;
        document.getElementById(SubPrefix + "NewSubstitutionTypeHiddenField").value = "2";
        $("#playerOutDiv").fadeIn();
        $("#playerInDiv").hide();
        $("#playerChangeDiv").fadeIn();
        $("#teamChangeDiv").hide();
    }
    else {
        // assuming that this is team change
        document.getElementById(SubPrefix + "NewSubstitutionTypeHiddenField").value = "3";
        $("#playerOutDiv").hide();
        $("#playerInDiv").hide();
        $("#playerChangeDiv").hide();
        $("#teamChangeDiv").fadeIn();
    }
    
    $("#NewSubstitution").fadeIn();
    
}

// used to hide (interface only - no chahging of data fields) substitution
function HideSubstitution(SubNum) {
    $("#Substitution" + SubNum).hide();
}

function ShowPenArea() {
    // showing penalties area:
    $("#PenaltiesDiv").fadeIn(200);

    // setting penatly area open button to be yellow:
    $("#" + SubPrefix + "ShowPenaltyTakersButton").css("color", "yellow");

    // if subst open - close it:
    CancelSubCreation();
}

// used to show substitution - existing data fields are not changed
function ShowSubstitutiuon(SubNum) {
    
    var SubType = document.getElementById(SubPrefix + "Sub" + SubNum + "TypeHiddenField").value;
    if (SubType == "0") { HideSubstitution(SubNum); }
    else {
        if (SubType == "1") {
            $("#Sub" + SubNum + "PlayerOutArea").show();
            $("#Sub" + SubNum + "PlayerInArea").show();
            $("#Sub" + SubNum + "PlayerChangeArea").show();
            $("#Sub" + SubNum + "TeamArea").hide();
        }
        else if (SubType == "2") {
            $("#Sub" + SubNum + "PlayerOutArea").show();
            $("#Sub" + SubNum + "PlayerInArea").hide();
            $("#Sub" + SubNum + "PlayerChangeArea").show();
            $("#Sub" + SubNum + "TeamArea").hide();
        }
        else {
            // assuming team change
            $("#Sub" + SubNum + "PlayerOutArea").hide();
            $("#Sub" + SubNum + "PlayerInArea").hide();
            $("#Sub" + SubNum + "PlayerChangeArea").hide();
            $("#Sub" + SubNum + "TeamArea").show();
        }

        // now the areas are ready - showing the whole substitution div
        $("#Substitution" + SubNum).fadeIn(200);
    } 
}

function CopySub1toSub2(sub1Num, sub2Num) {
    // Copies substitution 1 onto sub2. Does not deal with showing them
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TypeHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "TypeHiddenField").value
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TypeHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "TypeHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TimeMinHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "TimeMinHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TimeMaxHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "TimeMaxHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PossessionMinHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "PossessionMinHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PossessionMaxHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "PossessionMaxHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "ScoreMinHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "ScoreMinHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "ScoreMaxHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "ScoreMaxHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerOutHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerOutHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerInHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerInHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPlayerPositionHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPlayerPositionHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPlayerInstructionHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPlayerInstructionHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPlayerPreferredActionHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPlayerPreferredActionHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewTeamAttitudeHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewTeamAttitudeHiddenField").value;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewTeamAggressivenessHiddenField").value = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewTeamAggressivenessHiddenField").value;

    document.getElementById(SubPrefix + "Sub" + sub2Num + "TimeLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "TimeLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PossessionLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "PossessionLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "ScoreLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "ScoreLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerOutLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerOutLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerOutTitleLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerInLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerInLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPositionLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPositionLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewInstructionLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewInstructionLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPreferredActionLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPreferredActionLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewAttitudeLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewAttitudeLabel").innerHTML;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewAggressivenessLabel").innerHTML = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewAggressivenessLabel").innerHTML;
}

function CleanSubAreas(SubNum) {
    // cleans substituion hidden fields and text
    document.getElementById(SubPrefix + "Sub" + SubNum + "TypeHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "TimeMinHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "TimeMaxHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PossessionMinHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PossessionMaxHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "ScoreMinHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "ScoreMaxHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PlayerOutHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PlayerInHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewPlayerPositionHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewPlayerInstructionHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewPlayerPreferredActionHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewTeamAttitudeHiddenField").value = "0";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewTeamAggressivenessHiddenField").value = "0";

    document.getElementById(SubPrefix + "Sub" + SubNum + "TimeLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PossessionLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "ScoreLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PlayerOutLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "PlayerInLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewPositionLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewInstructionLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewPreferredActionLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewAttitudeLabel").innerHTML = " ";
    document.getElementById(SubPrefix + "Sub" + SubNum + "NewAggressivenessLabel").innerHTML = " ";

    // close:
    HideSubstitution(SubNum);
}

// used to delete an substitution. Included clearing data, closing divs, and them
function DeleteSubstitution(SubNum) {
    var NextSubNum = parseInt(SubNum) + 1;
    if (confirm(document.getElementById(SubPrefix + "AreYouSureDeleteInstructionHiddenField").value)) {
        if (SubNum != TotalNumberOfSubs) {
            var SubstitutionType = document.getElementById(SubPrefix + "Sub" + NextSubNum + "TypeHiddenField").value;
            // copying next substitutions to these substitutions till find empty one or till substitutions limit is reached.    
            while ((SubstitutionType != "0") && (NextSubNum <= TotalNumberOfSubs)) {
                // copy the next substitution to this one
                CopySub1toSub2(NextSubNum, SubNum);

                // showing the correct divs in current substitution
                ShowSubstitutiuon(SubNum);

                // increase the substitutiuon numbers
                SubNum++;
                NextSubNum++;
            }
        }

        // cleaning the last substitution - it is not needed any more.
        CleanSubAreas(SubNum);
        InitSubArrows();
    }
}

function OpenEnabledSubstitutionDivs() {
    var SubType;
    for (var i = 1; i <= TotalNumberOfSubs; i++) {
        SubType = document.getElementById(SubPrefix + "Sub" + i + "TypeHiddenField").value;
        if (SubType != "0") {
            document.getElementById(SubPrefix + "Sub" + i + "TimeLabel").innerHTML =
            GetTimeFromTillString(document.getElementById(SubPrefix + "Sub" + i + "TimeMinHiddenField").value,
                                  document.getElementById(SubPrefix + "Sub" + i + "TimeMaxHiddenField").value);
            document.getElementById(SubPrefix + "Sub" + i + "PossessionLabel").innerHTML =
            GetPossessionFromUptoString(document.getElementById(SubPrefix + "Sub" + i + "PossessionMinHiddenField").value,
                                        document.getElementById(SubPrefix + "Sub" + i + "PossessionMaxHiddenField").value);
            document.getElementById(SubPrefix + "Sub" + i + "ScoreLabel").innerHTML =
            GetGoalsConditionFromUptoString(document.getElementById(SubPrefix + "Sub" + i + "ScoreMinHiddenField").value,
                                            document.getElementById(SubPrefix + "Sub" + i + "ScoreMaxHiddenField").value);
            if (SubType == "1") {
                document.getElementById(SubPrefix + "Sub" + i + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "PlayerOutHiddenField").value;
            }
            else {
                document.getElementById(SubPrefix + "Sub" + i + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "PlayerHiddenField").value;
            }                                   
        }
        ShowSubstitutiuon(i);    
    }    
}


// finds next available substitution number
function FindNextAvailableSub() {
    var i = 1;
    var Found = false;
    var Type;
    while ((i <= TotalNumberOfSubs) && (!Found)) {
        Type = document.getElementById(SubPrefix + "Sub" + i + "TypeHiddenField").value;
        if (Type == "0") {
            Found = true;
        }
        else {
            i++;
        }
    }
    if (!Found) { return ("0"); }
    return (i);

}

function ValidateSub(SubType) {
    var ReturnValue = true;
    var DDLIn;
    var DDLOut;
    if (SubType == "1") {
        // substitution
        // only validating player in here
        DDLIn = document.getElementById(SubPrefix + "PlayerInDropDownList");
        
        if (DDLIn.selectedIndex == 0) {
            alert(document.getElementById(SubPrefix + "PleaseSelectPlayerInHiddenField").value);
            ReturnValue = false;
        }
        DDLOut = document.getElementById(SubPrefix + "PlayerOutDropDownList");
        if (DDLOut.selectedIndex == 0) {
            alert(document.getElementById(SubPrefix + "PleaseSelectPlayerOutHiddenField").value);
            ReturnValue = false;
        }
    }

    else if (SubType == "2") {
        // validating player out
        DDLOut = document.getElementById(SubPrefix + "PlayerOutDropDownList");
        if (DDLOut.selectedIndex == 0) {
            alert(document.getElementById(SubPrefix + "PleaseSelectPlayerHiddenField").value);
            ReturnValue = false;
        }
    }
    
    return (ReturnValue);
}

function SubmitNewSub() {
    var NewSubNumber = FindNextAvailableSub();

    // remove active class from buttons:
    $("#" + SubPrefix + "ShowSubstitutionButton").css("color", "white");
    $("#" + SubPrefix + "ShowPlayerChangeButton").css("color", "white");
    $("#" + SubPrefix + "ShowTeamChangeButton").css("color", "white");

    if (NewSubNumber == "0") {
        alert(document.getElementById(SubPrefix + "ReachedMaxSubstitutionsNumberHiddenField").value);
    }
    else {
        
        // copying new substitution to the empty place. Including Hidden fields init for server submit, and text fields init for presentation
        // Type
        var SubType = document.getElementById(SubPrefix + "NewSubstitutionTypeHiddenField").value;
        if (ValidateSub(SubType)) {
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "TypeHiddenField").value = SubType;

            // Time condition
            var SubstitutionTime = $("#SubstitutionTimeSlider").slider("option", "values");
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "TimeMinHiddenField").value = SubstitutionTime[0];
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "TimeMaxHiddenField").value = SubstitutionTime[1];
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "TimeLabel").innerHTML = GetTimeFromTillString(SubstitutionTime[0], SubstitutionTime[1]);

            // possession condition
            var PossessionCondition = $("#PossessionSlider").slider("option", "values");
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PossessionMinHiddenField").value = PossessionCondition[0];
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PossessionMaxHiddenField").value = PossessionCondition[1];
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PossessionLabel").innerHTML = GetPossessionFromUptoString(PossessionCondition[0], PossessionCondition[1]);

            // Score condition
            var ScoreCondition = $("#GoalsConditionSlider").slider("option", "values");
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "ScoreMinHiddenField").value = ScoreCondition[0];
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "ScoreMaxHiddenField").value = ScoreCondition[1];
            document.getElementById(SubPrefix + "Sub" + NewSubNumber + "ScoreLabel").innerHTML = GetGoalsConditionFromUptoString(ScoreCondition[0], ScoreCondition[1]);

            // Cards conditions = TBD

            // player out
            if (SubType == "1" || SubType == "2") {
                // set player out option - hidden field and label text
                if (SubType == "1") {
                    document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "PlayerOutHiddenField").value;
                }
                else {
                    document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerOutTitleLabel").innerHTML = document.getElementById(SubPrefix + "PlayerHiddenField").value;
                }
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerOutHiddenField").value = document.getElementById(SubPrefix + "PlayerOutDropDownList").options[document.getElementById(SubPrefix + "PlayerOutDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerOutLabel").innerHTML = document.getElementById(SubPrefix + "PlayerOutDropDownList").options[document.getElementById(SubPrefix + "PlayerOutDropDownList").selectedIndex].text;                
            }
            else {
                // set player out option to be 0 - just in case
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerOutHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerOutLabel").innerHTML = " ";
            }
            
            // PlayerIn
            if (SubType == "1") {
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerInHiddenField").value = document.getElementById(SubPrefix + "PlayerInDropDownList").options[document.getElementById(SubPrefix + "PlayerInDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerInLabel").innerHTML = document.getElementById(SubPrefix + "PlayerInDropDownList").options[document.getElementById(SubPrefix + "PlayerInDropDownList").selectedIndex].text;

            }
            else {
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerInHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "PlayerInLabel").innerHTML = " ";
            }
            if (SubType == "1" || SubType == "2") {
                // New player position
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPlayerPositionHiddenField").value = document.getElementById(SubPrefix + "PlayerPositionBehaviourDropDownList").options[document.getElementById(SubPrefix + "PlayerPositionBehaviourDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPositionLabel").innerHTML = document.getElementById(SubPrefix + "PlayerPositionBehaviourDropDownList").options[document.getElementById(SubPrefix + "PlayerPositionBehaviourDropDownList").selectedIndex].text;

                // New Player instruction
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPlayerInstructionHiddenField").value = document.getElementById(SubPrefix + "PlayerInstructionDropDownList").options[document.getElementById(SubPrefix + "PlayerInstructionDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewInstructionLabel").innerHTML = document.getElementById(SubPrefix + "PlayerInstructionDropDownList").options[document.getElementById(SubPrefix + "PlayerInstructionDropDownList").selectedIndex].text;

                // new player preferred action
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPlayerPreferredActionHiddenField").value = document.getElementById(SubPrefix + "PlayerPreferredActionBehaviourDropDownList").options[document.getElementById(SubPrefix + "PlayerPreferredActionBehaviourDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPreferredActionLabel").innerHTML = document.getElementById(SubPrefix + "PlayerPreferredActionBehaviourDropDownList").options[document.getElementById(SubPrefix + "PlayerPreferredActionBehaviourDropDownList").selectedIndex].text;

            }

            else {
                // init player position, instruction and preferred action
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPlayerPositionHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPositionLabel").innerHTML = " ";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPlayerInstructionHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewInstructionLabel").innerHTML = " ";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPlayerPreferredActionHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewPreferredActionLabel").innerHTML = " ";
            }

            if (SubType == "3") {
                // new Team attitude
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewTeamAttitudeHiddenField").value = document.getElementById(SubPrefix + "TeamNewAttitudeBehaviourDropDownList").options[document.getElementById(SubPrefix + "TeamNewAttitudeBehaviourDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewAttitudeLabel").innerHTML = document.getElementById(SubPrefix + "TeamNewAttitudeBehaviourDropDownList").options[document.getElementById(SubPrefix + "TeamNewAttitudeBehaviourDropDownList").selectedIndex].text;
                // new team Aggressiveness.
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewTeamAggressivenessHiddenField").value = document.getElementById(SubPrefix + "TeamAggressivenessBehaviourDropDownList").options[document.getElementById(SubPrefix + "TeamAggressivenessBehaviourDropDownList").selectedIndex].value;
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewAggressivenessLabel").innerHTML = document.getElementById(SubPrefix + "TeamAggressivenessBehaviourDropDownList").options[document.getElementById(SubPrefix + "TeamAggressivenessBehaviourDropDownList").selectedIndex].text;
            }
            else {
                // init team attitude and aggressiveness
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewTeamAttitudeHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewAttitudeLabel").innerHTML = " ";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewTeamAggressivenessHiddenField").value = "0";
                document.getElementById(SubPrefix + "Sub" + NewSubNumber + "NewAggressivenessLabel").innerHTML = " ";
            }

            // Show substitution
            ShowSubstitutiuon(NewSubNumber);

            // hide new substitution area:
            CloseOpenDiv("NewSubstitution", "AddNewSub")
            
            // Init sub arrows
            InitSubArrows();
        }
    }
}

function SwapSubs(sub1Num, sub2Num) {
    // Swaps substitutions. Deals with showing them in their new places
    var TypeHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "TypeHiddenField").value;
    var TimeMinHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "TimeMinHiddenField").value;
    var TimeMaxHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "TimeMaxHiddenField").value;
    var PossessionMinHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "PossessionMinHiddenField").value;
    var PossessionMaxHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "PossessionMaxHiddenField").value;
    var ScoreMinHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "ScoreMinHiddenField").value;
    var ScoreMaxHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "ScoreMaxHiddenField").value;
    var PlayerOutHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerOutHiddenField").value;
    var PlayerInHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerInHiddenField").value;
    var NewPlayerPositionHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPlayerPositionHiddenField").value;
    var NewPlayerInstructionHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPlayerInstructionHiddenField").value;
    var NewPlayerPreferredActionHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPlayerPreferredActionHiddenField").value;
    var NewTeamAttitudeHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewTeamAttitudeHiddenField").value;
    var NewTeamAggressivenessHiddenField = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewTeamAggressivenessHiddenField").value;

    var TimeLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "TimeLabel").innerHTML;
    var PossessionLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "PossessionLabel").innerHTML;
    var ScoreLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "ScoreLabel").innerHTML;
    var PlayerOutLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerOutLabel").innerHTML;
    var PlayerOutTitleLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerOutTitleLabel").innerHTML
    var PlayerInLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "PlayerInLabel").innerHTML;
    var NewPositionLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPositionLabel").innerHTML;
    var NewInstructionLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewInstructionLabel").innerHTML;
    var NewPreferredActionLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewPreferredActionLabel").innerHTML;
    var NewAttitudeLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewAttitudeLabel").innerHTML;
    var NewAggressivenessLabel = document.getElementById(SubPrefix + "Sub" + sub1Num + "NewAggressivenessLabel").innerHTML;

    // copy sub2 onto sub1:
    CopySub1toSub2(sub2Num, sub1Num);

    // set sub2 to be what sub1 once was:
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TypeHiddenField").value = TypeHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TimeMinHiddenField").value = TimeMinHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "TimeMaxHiddenField").value = TimeMaxHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PossessionMinHiddenField").value = PossessionMinHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PossessionMaxHiddenField").value = PossessionMaxHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "ScoreMinHiddenField").value = ScoreMinHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "ScoreMaxHiddenField").value = ScoreMaxHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerOutHiddenField").value = PlayerOutHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerInHiddenField").value = PlayerInHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPlayerPositionHiddenField").value = NewPlayerPositionHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPlayerInstructionHiddenField").value = NewPlayerInstructionHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPlayerPreferredActionHiddenField").value = NewPlayerPreferredActionHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewTeamAttitudeHiddenField").value = NewTeamAttitudeHiddenField;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewTeamAggressivenessHiddenField").value = NewTeamAggressivenessHiddenField;

    document.getElementById(SubPrefix + "Sub" + sub2Num + "TimeLabel").innerHTML = TimeLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PossessionLabel").innerHTML = PossessionLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "ScoreLabel").innerHTML = ScoreLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerOutLabel").innerHTML = PlayerOutLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerOutTitleLabel").innerHTML = PlayerOutTitleLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "PlayerInLabel").innerHTML = PlayerInLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPositionLabel").innerHTML = NewPositionLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewInstructionLabel").innerHTML = NewInstructionLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewPreferredActionLabel").innerHTML = NewPreferredActionLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewAttitudeLabel").innerHTML = NewAttitudeLabel;
    document.getElementById(SubPrefix + "Sub" + sub2Num + "NewAggressivenessLabel").innerHTML = NewAggressivenessLabel;

    // show the subs correctly:
    ShowSubstitutiuon(sub1Num);
    ShowSubstitutiuon(sub2Num);
    InitSubArrows();
}

function MoveSubDown(SubNum) {
    var Num = parseInt(SubNum);
    SwapSubs(Num, Num + 1);
}

function MoveSubUp(SubNum) {
    var Num = parseInt(SubNum);
    SwapSubs(Num - 1, Num);
}



$(function() {
    $("#SubstitutionTimeSlider").slider({
        range: true,
        min: 0,
        max: 120,
        values: [0, 120],
        animate: 200,
        slide: function(event, ui) {
            document.getElementById(SubPrefix + "SubstitutionTimeLabel").innerHTML = GetTimeFromTillString(ui.values[0],ui.values[1]);
        }
    });

    $("#PossessionSlider").slider({
        range: true,
        min: 0,
        max: 100,
        values: [0, 100],
        animate: 200,
        slide: function(event, ui) {
            document.getElementById(SubPrefix + "PosesseionConditionLabel").innerHTML = GetPossessionFromUptoString(ui.values[0], ui.values[1]);
        }
    });

    $("#GoalsConditionSlider").slider({
        range: true,
        min: -10,
        max: 10,
        values: [-10, 10],
        animate: 200,
        slide: function(event, ui) {
            document.getElementById(SubPrefix + "GoalsConditionLabel").innerHTML = GetGoalsConditionFromUptoString(ui.values[0],ui.values[1]);
        }
    });
});